/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import { FileSet } from '@ohos/hvigor';
import { TaskDetails } from '@ohos/hvigor';
import { TaskInputValue } from '@ohos/hvigor';

import { SignTypeEnum } from '../../enum/sign-type-enum.js';
import { ProjectBuildProfile } from '../../options/build/project-build-profile.js';
import { TargetTaskService } from '../service/target-task-service.js';
import { SignUtil } from '../sign/sign-util.js';
import { OhosHapTask } from '../task/ohos-hap-task.js';
import SigningConfigBuildOpt = ProjectBuildProfile.SigningConfigBuildOpt;
import { OhosLogger } from '../../utils/log/ohos-logger.js';

/**
 * hap级别的签名基础类, 继承 OhosHapTask
 *
 * @since 2022/10/16
 */
export abstract class BaseSign extends OhosHapTask {
  public readonly log: OhosLogger = OhosLogger.getLoggerWithDurationId(BaseSign.name, this.durationEvent.getId());
  protected readonly signingConfig: SigningConfigBuildOpt | undefined;
  protected readonly signUtil: SignUtil;
  protected readonly signType: SignTypeEnum;

  protected constructor(targetService: TargetTaskService, taskDetails: TaskDetails, signType?: SignTypeEnum) {
    super(targetService, taskDetails);
    this.signType = signType ?? (this.targetData.isHarmonyOS() ? SignTypeEnum.HOS_HAP : SignTypeEnum.HAP);
    this.signUtil = new SignUtil(this.service, this.signType, this.pathInfo, this.targetData.getProduct(), this.sdkInfo);
    this.signingConfig = this.signUtil._signingConfig;
  }

  async beforeAlwaysAction() {
    this.log.warn(this.signUtil._signingConfigCheckLogStr);
  }

  declareInputs(): Map<string, TaskInputValue> {
    return SignUtil.getSigningConfigInputs(this.sdkInfo, this.signingConfig!);
  }

  declareInputFiles(): FileSet {
    return this.signUtil.getSigningConfigInputFiles();
  }

  abstract initTaskDepends(): void;

  protected abstract doTaskAction(): void;
}
